package com.eelye.util.winui.window;

import com.eelye.util.winui.manager.ProcessManager;
import com.eelye.util.winui.manager.WindowManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class WindowTest_UT {

    private Process process;

    @AfterEach
    void terminateProcess() {
        if (null == process) {
            return ;
        }

        process.terminate();
    }

    @Nested
    class getProcess {

        @Test
        void 测试获取窗口的text_应该能获取到() throws Exception {
            process = ProcessManager.launchProcess("C:\\Windows\\system32\\mstsc.exe");

            String clsName = "#32770";
            String text = "远程桌面连接";
            Window top = WindowManager.findTopWindow(clsName, false, text, false, 5_000);
            assertNotNull(top);

            Process process1 = top.getProcess();

            assertEquals(process.getId(), process1.getId());
        }
    }

    @Nested
    class setFocus {

        @Test
        void 测试窗口被设置focus之后_获取当前窗口_其class应该正确() throws Exception {
            // 用开始菜单作测试，因为启动测试时，开始菜单肯定不是focus的。
            String clsName = "Shell_TrayWnd";
            String text = "";
            Window top = WindowManager.findTopWindow(clsName, false, text, false);
            assertNotNull(top);

            top.setFocus();

            Window currentWnd = WindowManager.getCurrentWindow();

            assertNotNull(currentWnd);
            assertEquals(clsName, currentWnd.getClassName());
        }
    }

    @Nested
    class findChild {

        @Test
        void 测试直接子窗口_应该找到并且hWnd不为空() throws Exception {
            process = ProcessManager.launchProcess("C:\\Windows\\system32\\mstsc.exe");
            String clsName = "#32770";
            String text = "远程桌面连接";
            Window top = WindowManager.findTopWindow(clsName, false, text, false, 5_000);

            assertNotNull(top);

            int id = 0x1394;
            String subClsName = "Edit";
            Window edit = top.findChild(id, subClsName, true, 1_000);

            assertNotNull(edit);
            assertNotNull(edit.getHWnd());
        }

        @Test
        void 测试孙子窗口_应该找到并且hWnd不为空() throws Exception {
            process = ProcessManager.launchProcess("C:\\Windows\\system32\\mstsc.exe");
            String clsName = "#32770";
            String text = "远程桌面连接";
            Window top = WindowManager.findTopWindow(clsName, false, text, false, 5_000);

            assertNotNull(top);

            int id = 0x3306;
            String subClsName = "SysLink";
            Window sub = top.findChild(id, subClsName, true, 1_000);

            assertNotNull(sub);
            assertNotNull(sub.getHWnd());
        }

        @Test
        void 测试找到窗口后_实例的类型正确() throws Exception {
            process = ProcessManager.launchProcess("C:\\Windows\\system32\\mstsc.exe");
            String clsName = "#32770";
            String text = "远程桌面连接";
            Window top = WindowManager.findTopWindow(clsName, false, text, false, 5_000);

            assertNotNull(top);

            int id = 0x1394;
            String subClsName = "Edit";
            Window edit = top.findChild(id, subClsName, true, 1_000);

            assertNotNull(edit);
            assertTrue(edit instanceof Edit);
        }

        @Test
        void 测试id不存在_class存在_应该找不到窗口() throws Exception {
            process = ProcessManager.launchProcess("C:\\Windows\\system32\\mstsc.exe");
            String clsName = "#32770";
            String text = "远程桌面连接";
            Window top = WindowManager.findTopWindow(clsName, false, text, false, 5_000);

            assertNotNull(top);

            int id = 0x673306;
            String subClsName = "SysLink";
            Window edit = top.findChild(id, subClsName, true);

            assertNull(edit);
        }

        @Test
        void 测试id存在_class不存在_应该找不到窗口() throws Exception {
            process = ProcessManager.launchProcess("C:\\Windows\\system32\\mstsc.exe");
            System.out.println(process.getId());
            String clsName = "#32770";
            String text = "远程桌面连接";
            Window top = WindowManager.findTopWindow(clsName, false, text, false, 5_000);

            assertNotNull(top);

            int id = 0x3306;
            String subClsName = "SysLink1";
            Window edit = top.findChild(id, subClsName, true);

            assertNull(edit);
        }
    }
}